package starcoast.bocai.com.starcoast.activitys.user;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import starcoast.bocai.com.starcoast.R;
import starcoast.bocai.com.starcoast.activitys.SearchActivity;
import starcoast.bocai.com.starcoast.common.BaseActivity;
import starcoast.bocai.com.starcoast.fragments.PropertyComplaintsFragment;
import starcoast.bocai.com.starcoast.fragments.PropertyRepairFragment;
import starcoast.bocai.com.starcoast.view.MyViewPager;
import starcoast.bocai.com.starcoast.view.ViewFindUtils;

/**
 * Created by admin on 2016/6/16.
 */
public class PropertyActivity extends BaseActivity {
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private final String[] mTitles = {
            "维修", "投诉"
    };
    private MyPagerAdapter mAdapter;
    @Bind(R.id.my_property_tab)
    SlidingTabLayout my_property_tab;
    private int index = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_property);
        ButterKnife.bind(this);
        initToolbar("物业服务");
        initToolBack();

        initView();
    }
    private void initView(){
        toolbar.inflateMenu(R.menu.menu_add);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if (index == 1) {
                    startActivity(new Intent(PropertyActivity.this, MyComplainActivity.class));
                }else{
                    startActivity(new Intent(PropertyActivity.this, MyRepirActivity.class));
                }
                return false;
            }
        });
        mFragments.add(new PropertyRepairFragment());
        mFragments.add(new PropertyComplaintsFragment());
        View decorView = getWindow().getDecorView();
        MyViewPager vp = ViewFindUtils.find(decorView, R.id.vp);
        mAdapter = new MyPagerAdapter(getSupportFragmentManager());
        vp.setAdapter(mAdapter);
        my_property_tab.setViewPager(vp);
        vp.setCurrentItem(0);
        my_property_tab.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                Log.e("onTabSelect",position+"     onTabSelect");
                index = position;
            }

            @Override
            public void onTabReselect(int position) {
            }
        });

    }
    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }
}
